package com.contactbook.web.client;

import com.contactbook.web.client.dto.ContactDTO;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class DeleteContactWidget extends View {
	
	private class OkHandler implements ClickHandler {
		
		private int groupId;
		
		public OkHandler(int groupId) {
			this.groupId = groupId;
		}

		@Override
		public void onClick(ClickEvent arg0) {
			deleteContact(groupId);			
		}		
	}
	
	private class CancelHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent arg0) {
			contactBook.switchView("listOfContacts");			
		}		
	}
	
	private VerticalPanel content;
	
	private static final String TITLE = "Delete a contact";
	
	public DeleteContactWidget(ContactBook contactBook) {
		super(contactBook);
		content = new VerticalPanel();
	}
	
	@SuppressWarnings("deprecation")
	@Override
	public VerticalPanel getContent() {
		if (contactBook.isLogged()) {
			if (contactBook.getCurrentSession().parameters.get((String)Constants.SessionParameterContactId) != null) {
				int contactId = (Integer)contactBook.getCurrentSession().parameters.get((String)Constants.SessionParameterContactId);
				contactBook.getCurrentSession().clearParameter((String)Constants.SessionParameterContactId);
				checkContactId(contactId);
				return buildView();
			} else {
				content = new ErrorPanel();
			}
		} else
			contactBook.switchView("login");
		return null;
	}


	private VerticalPanel buildView() {
		return content;
	}

	@Override
	public String getTitle() {
		return TITLE;
	}
	
	private void checkContactId(int contactId) {
		AsyncCallback<ContactDTO> callback = new AsyncCallback<ContactDTO>() {

			@SuppressWarnings("deprecation")
			@Override
			public void onFailure(Throwable arg0) {
				content.clear();
				content.add(new ErrorPanel());
			}

			@Override
			public void onSuccess(ContactDTO contactDTO) {
				
				content.clear();
				
				DecoratorPanel panelDelete = new DecoratorPanel();
				VerticalPanel panelWidgets = new VerticalPanel();
				
				HorizontalPanel panelLabelConfirm = new HorizontalPanel();
				panelLabelConfirm.setWidth("290px");
				panelLabelConfirm.setHeight("90px");
				
				Label labelConfirm = new Label();
				String message = "You are going to delete the contact '";
				labelConfirm.setStyleName("gwt-label-labelnote");
				
				message += contactDTO.getFirstName() + " " + contactDTO.getLastName();
				message += "' from your contact book. Do you confirm that you want to delete this contact ?";
				labelConfirm.setText(message);
				
				panelLabelConfirm.add(labelConfirm);
				
				panelWidgets.add(panelLabelConfirm);
				
				FlexTable tableButtons = new FlexTable();
				tableButtons.setWidth("290px");
				
				Button okButton = new Button("Ok");
				okButton.setStyleName("buttonform");
				okButton.addClickHandler(new OkHandler(contactDTO.getId()));
				
				Button cancelButton = new Button("Back");
				cancelButton.setStyleName("buttonform");
				cancelButton.addClickHandler(new CancelHandler());
				
				tableButtons.setWidget(0, 0, okButton);
				tableButtons.setWidget(0, 1, cancelButton);
				
				/* Table buttons properties */
				tableButtons.getFlexCellFormatter().setHeight(0, 0, "60px");
				
				tableButtons.getFlexCellFormatter().setAlignment(0, 0, ALIGN_CENTER, ALIGN_MIDDLE);
				tableButtons.getFlexCellFormatter().setAlignment(0, 1, ALIGN_CENTER, ALIGN_MIDDLE);
				
				tableButtons.setCellPadding(8);
				
				panelWidgets.add(tableButtons);
				panelWidgets.setSpacing(10);
				panelDelete.add(panelWidgets);
				
				content.add(panelDelete);
			}
			
		};
		contactBook.getContactService().getContactById(contactId, contactBook.getCurrentSession().getLogin(), contactBook.getCurrentSession().getPassword(), callback);
	}
	
	private void deleteContact(int contactId) {
		AsyncCallback<Void> callback = new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable arg0) {				
			}

			@Override
			public void onSuccess(Void arg0) {
				contactBook.switchView("listOfContacts");
			}			
		};
		contactBook.getContactService().delete(contactId, contactBook.getCurrentSession().getLogin(), contactBook.getCurrentSession().getPassword(), callback);
	}
}
